package core

import (
	"fmt"
	"github.com/spf13/viper"
	"log"
	"os"
)

var config *viper.Viper

func LoadConfig() *viper.Viper {
	v := viper.New()
	v.SetConfigFile("./config.yml")
	v.SetConfigType("yaml")
	err := v.ReadInConfig() // 查找并读取配置文件
	if err != nil {         // 处理读取配置文件的错误
		panic(fmt.Errorf("Fatal error config file: %s \n", err))
	}
	return v
}

func GetConfig() *viper.Viper {
	if config == nil {
		config = LoadConfig()
	}
	return config
}

func GetLogDir() string {
	logFilePath := ""
	if dir, err := os.Getwd(); err == nil {
		logFilePath = dir + "/logs/"
	}
	_, err := os.Stat(logFilePath)
	if os.IsNotExist(err) {
		if err := os.MkdirAll(logFilePath, 0777); err != nil {
			log.Println(err.Error())
		}
	}
	return logFilePath
}
